#include <stdio.h>
#include <assert.h>
int expand(char* ,char*,int );
void expandRange(int , char , char , char* ,int);
int clear(char* c, int count);
#define MAGIC_1 2048
int main()
{ 
  char b[MAGIC_1];
  clear(b,MAGIC_1);
  expand("lol what is this bullshit a-z",b,1);
  printf("\n%s\n",b);
  clear(b,MAGIC_1);
  expand("lol what is this bullshit a-c",b,1);
  printf("\n%s\n",b);
  clear(b,MAGIC_1);
  expand("lol what is this bullshit a-b-c",b,1);
  printf("\n%s\n",b);
  clear(b,MAGIC_1);
  expand("lol what is this bullshit 0-a",b,0);
  printf("\n%s\n",b);
  clear(b,MAGIC_1);
  return 0;    
} 

int clear(char* c, int count)
{
  for (int i=0; i<count;i++)
    {c[i]=0;}
  return 0;
}
/* in string s1 if a - is detected.
    1) check before the -.  
       if it's a space, treat it like normal
       if it's not a space, treat this as the beginning of the range
          check after the - 
          expand for one until the other.
       if it's the end of a range treat it like normal

/* a-z turns to abcdef...z */
/* case and digits allowed */
/* and be prepared to handle cases like a-b-c and a-z0-9 and -a-z. */
/* Arrange that a leading or trailing - is taken literally. */
/* s2 should be initialized to 0*/
int expand(char* s1,char* s2, int largerange)
{
// s2 has to be big enough 
  if (s1 == 0)
    {
	 s2=0;
	 return -1;
    }
  for (int i=0,j=0,last_match=0;s1[i]!=0;i++,j++)
    {
	 switch(s1[i])
	   {
	   case '-':
		if ((i!=0) && (last_match!=(i-1)) && (s1[i+1]!=0)) // we're good to expand
		  {
		    //we should be checking for digits/alphanumerics here somewhere
		    int count = (int)(s1[i+1]-s1[i-1])+1;
		    char newc[count] ;	    
		    expandRange(count,s1[i-1],s1[i+1],newc,largerange);
		    for (int k=1;k<count; k++) //we don't need the first one, we've already got it
			 {
			   s2[j]=newc[k];
			   j++;
			 }
    		    j--; // for some reason this goes one to far.
		    last_match=i+1;
		    i++; //gets rid of -
		    continue;
		  }
		else if (last_match ==(i-1)) //ie we just finished one
		  {
		    s2[j]=s1[i];
		    continue;
		  };  //drop through to default
	   default:
		s2[j]=s1[i];
	   }
    }
  return 0;
}

void expandRange(int count, char c1, char c2, char* newc, int largerange)
{
  if (largerange)
    if (c1 >= 'a')
	 assert (c2 <= 'z');
    else if (c1 >= 'A')
	 assert (c2 <= 'Z');
    else if (c1 >= '0')
	 assert (c2 <= '9');
  
  for (int i =0, j=(int)c1, end=(int)c2; i < count; i++, j++)
	 newc[i]=j;
}
